﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
using System.Web.Script.Serialization;

namespace CommandMgr.Sdk
{
    public class CommandMgr
    {
        string _apiUrl;
        RestClient client;

        public CommandMgr(string apiUrl)
        {
            _apiUrl = apiUrl;
            client = new RestClient(_apiUrl);
        }

        public bool Dequeue(string commandName)
        {
            bool result = false;
            var request = new RestRequest("CommandQueue/{id}", Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddUrlSegment("id", commandName);

            RestResponse response = (RestResponse)client.Execute(request);
            result = response.Content.Equals("true");
            return result;
        }

        public void Queue(string commandName)
        {
            var request = new RestRequest("CommandQueue/{id}", Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddUrlSegment("id", commandName);

            RestResponse response = (RestResponse)client.Execute(request);
         }

        public List<Command> ListCommands()
        {
            List<Command> commands = new List<Command>();
            var request = new RestRequest("Commands", Method.GET);
            request.RequestFormat = DataFormat.Json;
            RestResponse response = (RestResponse)client.Execute(request);
            string json = response.Content;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            commands = serializer.Deserialize<List<Command>>(json);

            return commands;
        }

    }
}
